﻿Public Class MASCUSTOMER
    Private IDMASCUSTOMER As Integer
    Private CODE As Integer
    Private NAME As String
    Private CID As String
    Private TELL As String
    Private IDMASAMPUR As Integer
    Private IDMASCHANGWAT As Integer
    Private ADDRESS As String
    Private EMAIL As String
    Private TAX As String
    Private REGISTRATION As String
    Private CREDIT As String
    Private STATUS = Nothing
    Private SelectDatagridMascustomer = Nothing
    Dim connect As CONDB = CONDB.NewConnection
    Property SelectDatagridMascustomer_() As String
        Get
            SelectDatagridMascustomer = "SELECT `code` as 'CODE',`idmascustomer` as 'id',`name` as 'ชื่อลูกค้า',`cid` as 'เลขบัตรประชาชน',`tell` as 'เบอร์',masampur.`idmasampur`,masampur.`ampur` as 'อำเภอ',maschangwat.`idmaschangwat`,maschangwat.`changwat` as 'จังหวัด',`address` as 'ที่อยู่',`email`,`tax`,`registration`,`status` as 'สถานะการเปิดใช้งาน',`credit` FROM cpm.mascustomer "
            SelectDatagridMascustomer += "LEFT JOIN (SELECT `idmasampur`,`ampur` FROM cpm.masampur WHERE `status` = 1) as masampur on mascustomer.`idmasampur` = masampur.`idmasampur` "
            SelectDatagridMascustomer += "LEFT JOIN (SELECT `idmaschangwat`,`changwat` FROM cpm.maschangwat WHERE `status` = 1) as maschangwat on mascustomer.`idmaschangwat` = maschangwat.`idmaschangwat`;"
            Return SelectDatagridMascustomer
        End Get
        Set(value As String)

        End Set
    End Property
    Property STATUS_() As Boolean
        Get
            Return STATUS
        End Get
        Set(value As Boolean)
            If value = True Then
                STATUS = 1
            Else
                STATUS = 0
            End If
        End Set
    End Property
    Property CREDIT_() As String
        Get
            Return CREDIT
        End Get
        Set(value As String)
            CREDIT = value
        End Set
    End Property
    Property NAME_() As String
        Get
            Return NAME
        End Get
        Set(value As String)
            NAME = value
        End Set
    End Property
    Property CID_() As String
        Get
            Return CID
        End Get
        Set(value As String)
            CID = value
        End Set
    End Property
    Property TELL_() As String
        Get
            Return TELL
        End Get
        Set(value As String)
            TELL = value
        End Set
    End Property
    Property CODE_() As Integer
        Get
            Return CODE
        End Get
        Set(value As Integer)
            CODE = value
        End Set
    End Property
    Property IDMASCUSTOMER_() As Integer
        Get
            Return IDMASCUSTOMER
        End Get
        Set(value As Integer)
            IDMASCUSTOMER = value
        End Set
    End Property
    Property IDMASAMPUR_() As Integer
        Get
            Return IDMASAMPUR
        End Get
        Set(value As Integer)
            IDMASAMPUR = value
        End Set
    End Property
    Property IDMASCHANGWAT_() As Integer
        Get
            Return IDMASCHANGWAT
        End Get
        Set(value As Integer)
            IDMASCHANGWAT = value
        End Set
    End Property
    Property ADDRESS_() As String
        Get
            Return ADDRESS
        End Get
        Set(value As String)
            ADDRESS = value
        End Set
    End Property
    Property EMAIL_() As String
        Get
            Return EMAIL
        End Get
        Set(value As String)
            EMAIL = value
        End Set
    End Property
    Property TAX_() As String
        Get
            Return TAX
        End Get
        Set(value As String)
            TAX = value
        End Set
    End Property
    Property REGISTRATION_() As String
        Get
            Return REGISTRATION
        End Get
        Set(value As String)
            REGISTRATION = value
        End Set
    End Property
    Public Sub INSERTMASCUSTOMER()
        connect = CONDB.NewConnection()
        connect.BeginTrans()
        Dim sql As String
        Try
            sql = "INSERT INTO cpm.mascustomer"
            sql += "(NAME,CID,TELL,IDMASCHANGWAT,IDMASAMPUR,ADDRESS,EMAIL,TAX,CREDIT,REGISTRATION,CODE) "
            sql += " VALUES ("
            sql += " '" & NAME & "' , "
            sql += " '" & CID & "' , "
            sql += " '" & TELL & "' , "
            If IDMASCHANGWAT = 0 Then
                sql += "Null,"
            Else
                sql += " '" & IDMASCHANGWAT & "' , "
            End If
            If IDMASAMPUR = 0 Then
                sql += "Null,"
            Else
                sql += " '" & IDMASAMPUR & "' , "
            End If
            sql += " '" & ADDRESS & "' , "
            sql += " '" & EMAIL & "' , "
            sql += " '" & TAX & "' , "
            sql += " '" & CREDIT & "' , "
            sql += " '" & REGISTRATION & "',"
            sql += " '" & CODE & "' ); "
            connect.ExecuteNonQuery(sql)
            connect.CommitTrans()
        Catch ex As Exception
            connect.RollbackTrans()
            MsgBox(ex.ToString)
        Finally
            connect.Dispose()
        End Try

    End Sub
    Public Sub UPDATEMASCUSTOMER()
        connect = CONDB.NewConnection()
        connect.BeginTrans()
        Dim sql As String
        Try
            sql = "UPDATE cpm.mascustomer SET "
            sql += " name = '" & NAME & "' , "
            sql += " cid = '" & CID & "' , "
            sql += " tell = '" & TELL & "' , "
            If IDMASAMPUR = 0 Then
                sql += "idmasampur = Null,"
            Else
                sql += " idmasampur = '" & IDMASAMPUR & "' , "
            End If
            If IDMASCHANGWAT = 0 Then
                sql += "idmaschangwat = Null,"
            Else
                sql += " idmaschangwat = '" & IDMASCHANGWAT & "' , "
            End If
            sql += " address = '" & ADDRESS & "' , "
            sql += " email = '" & EMAIL & "' , "
            sql += " tax = '" & TAX & "' , "
            sql += " code = '" & CODE & "',"
            sql += " registration = '" & REGISTRATION & "'  "
            sql += " WHERE  idmascustomer ='" & IDMASCUSTOMER & "'; "
            connect.ExecuteNonQuery(sql)
            connect.CommitTrans()
        Catch ex As Exception
            connect.RollbackTrans()
            MsgBox(ex.ToString)
        Finally
            connect.Dispose()
        End Try

    End Sub
End Class
